/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package g7.dao.impl;

import g7.dao.BacklogDAO;
import g7.modelo.Backlog;
import g7.modelo.Produto;
import g7.modelo.Sprint;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author benigno
 */
@Stateless
public class BacklogDAOImpl implements BacklogDAO {

    @PersistenceContext(name = "g7Scrum")
    private EntityManager manager;

    @Override
    public void salvar(Backlog backlog) {
        manager.merge(backlog);
    }

    @Override
    public void excluir(Backlog backlog) {
        manager.remove(manager.getReference(Backlog.class, backlog.getId()));
    }

    @Override
    public List<Backlog> listaBacklogs() {
        Query query = manager.createQuery("Select b from Backlog b");
        List<Backlog> backlogs = query.getResultList();
        return backlogs;
    }

    @Override
    public Backlog findById(Long id) {
        return manager.find(Backlog.class, id);
    }

    @Override
    public List<Backlog> recuperarBacklogProduto(Produto produto) {
        List<Backlog> backlogs = new ArrayList<Backlog>();

        Query query = manager.createQuery("Select b from Backlog b inner join"
                + " b.produto p where p.id =:id").setParameter("id", produto.getId());

        backlogs = query.getResultList();
        return backlogs;
    }

    @Override
    public List<Backlog> carregarBacklogsSprint(Sprint sprint) {
        List<Backlog> backlogs = new ArrayList<Backlog>();

        Query query = manager.createQuery("Select b from Sprint s inner join"
                + " s.backlogs b where s.id =:id").setParameter("id", sprint.getId());
        backlogs = query.getResultList();

        return backlogs;
    }
}
